﻿using System;
using System.Globalization;
using System.Linq;

namespace Medianamik.Core
{
    public class ManyToOneNodeProperty : NodeProperty
    {
        internal ManyToOneNodeProperty(NodeTypeProperty pNodeTypeProperty, object value, string cultureName, Node node)
            : base(pNodeTypeProperty, value, cultureName, node)
        {
        }

        internal ManyToOneNodeProperty(int? propertyId, NodeTypeProperty nodeTypeProperty, string value, string cultureName, int? revision, Node node)
            : base(propertyId, nodeTypeProperty, value, cultureName, revision, node)
        {
        }

        protected RelationOptionCollection GetOptions(CultureInfo culture)
        {
            if (NodeTypeProperty.PrimaryTypeID == null)
                throw new InvalidOperationException("ManyToOneNodeProperty.NodeTypeProperty.PrimaryTypeID can not be null");
            if (NodeTypeProperty.PrimaryPropertyID == null)
                throw new InvalidOperationException("ManyToOneNodeProperty.NodeTypeProperty.PrimaryPropertyID can not be empty");

            var nodes = NodeManager.GetNodesByType(NodeTypeProperty.PrimaryTypeID.Value)
                .Where(n => n.CultureName == culture.Name);

            var options = new RelationOptionCollection();
            foreach(var n in nodes)
            {
                options.Add(new ManyToOneRelation(n.NodeId, NodeTypeProperty.PrimaryPropertyID.Value));
            }
            
            return options;
        }

        public override IMedianamikControl GetMedianamikControl(bool pEnableClientScriptValidation,
            bool pAutoGenerateValidators, string tabName, bool pDisplayLabel, string pLabel, string pLabelLanguageExtensionText,
            string pLabelExtensionText, bool hideMultiLingualControls, bool hideNeutralLanguageIndication, bool? isRequired)
        {
            var control = base.GetMedianamikControl(pEnableClientScriptValidation,
                pAutoGenerateValidators, tabName, pDisplayLabel, pLabel, pLabelLanguageExtensionText, pLabelExtensionText,
                hideMultiLingualControls, hideNeutralLanguageIndication, isRequired);

            //Cette propriété est spécifique pour le controle OneToMany
            var editionCulture = MedianamikContext.Current.EditionCulture;
            control.OptionsDelegate = () => GetOptions(editionCulture);

            return control;
        }

        public override IMedianamikControl GetMedianamikControl(bool pEnableClientScriptValidation,
            bool pAutoGenerateValidators, string tabName, bool pDisplayLabel, string pLabel, string pLabelLanguageExtensionText,
            string pLabelExtensionText, bool hideMultiLingualControls, bool hideNeutralLanguageIndication, Guid medianamikControlId, bool? isRequired)
        {
            var control = base.GetMedianamikControl(pEnableClientScriptValidation,
                pAutoGenerateValidators, tabName, pDisplayLabel, pLabel, pLabelLanguageExtensionText, pLabelExtensionText,
                hideMultiLingualControls, hideNeutralLanguageIndication, medianamikControlId, isRequired);

            //Cette propriété est spécifique pour le controle OneToMany
            var editionCulture = MedianamikContext.Current.EditionCulture;
            control.OptionsDelegate = () => GetOptions(editionCulture);

            return control;
        }
    }
}